package com.ant.storage.service.impl;

import com.ant.storage.common.ResultCode;
import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.ReimbursementDto;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.AuditReverseListMapper;
import com.ant.storage.mapper.DictionaryMapper;
import com.ant.storage.pojo.*;
import com.ant.storage.mapper.ReimbursementMapper;
import com.ant.storage.service.IReimbursementService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author melon
 * @since 2022-07-26
 */
@Service
public class ReimbursementServiceImpl extends ServiceImpl<ReimbursementMapper, Reimbursement> implements IReimbursementService {

    @Resource
    private AuditReverseListMapper auditReverseListMapper;
    @Resource
    private DictionaryMapper dictionaryMapper;

    @Override
    public PageInfo<Reimbursement> getPageList(ReimbursementDto dto) {
        try {
            PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
            QueryWrapper<Reimbursement> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(dto.getSubscribeNumber())) {
                wrapper.lambda().eq(Reimbursement::getReimbursementName, dto.getSubscribeNumber());
            }
            if (StringUtils.isNotEmpty(dto.getStatus())) {
                wrapper.lambda().eq(Reimbursement::getHandlerStatus, dto.getStatus());
            }

            wrapper.orderByDesc("id");
            List<Reimbursement> reimbursementList = this.baseMapper.selectList(wrapper);
            List<Reimbursement> list = reimbursementList.stream().map(item -> {
                //审核人信息
                List<AuditReverseList> auditListList = auditReverseListMapper.selectList
                        (new QueryWrapper<AuditReverseList>().eq("parent_id", item.getId()));
                item.setAuditReverseList(auditListList);
                Dictionary dictionary =
                        dictionaryMapper.selectOne(new QueryWrapper<Dictionary>().lambda().eq(Dictionary::getId,
                                item.getReimbursementType()));
                item.setReimbursementType(dictionary.getTypeName());

                return item;
            }).collect(Collectors.toList());
            PageInfo<Reimbursement> pageInfo = new PageInfo<>(reimbursementList);
            pageInfo.setList(list);
            return pageInfo;

        } catch (Exception e) {
            throw new AntException(ResultCode.BODY_NOT_MATCH);
        }
    }

    @Override
    public ResultVo reimbursemenSave(Reimbursement re) {
        re.setCreationTime(new Date());
        this.baseMapper.insert(re);
        if (re.getAuditReverseList().size() > 0) {
            for (AuditReverseList a : re.getAuditReverseList()) {
                AuditReverseList aud = new AuditReverseList();
                aud.setParentId(re.getId());
                aud.setName(a.getName());
                aud.setTypes(a.getTypes());
                aud.setOpinion(a.getOpinion());
            }
        }
        return ResultVo.success("申请通过");
    }
}
